#! /bin/bash

pass=mrc2018
remote_directory=/home/rcmu/workspace/taomu


function config(){
	dfile=./startup_app.sh
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c200.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c221.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c201.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c202.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c223.taomu.cool:$remote_directory
	
	dfile=./taomu.yml
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c200.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c221.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c201.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c202.taomu.cool:$remote_directory
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c223.taomu.cool:$remote_directory
	dfile=./broker.yml
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c223.taomu.cool:$remote_directory
	dfile=./startup_mqtt.sh
	sshpass -p $pass scp -o StrictHostKeyChecking=no $dfile rcmu@c223.taomu.cool:$remote_directory
}


function publish(){
gradle clean build -x test && gradle publish  && gradle jC
cd ./taomu-app/build/libs
ls -l
cp taomu-app-*.jar deps
ls -l
tar zcvf taomu.tar.gz deps
ls -l
}

function deploy(){

echo "$remote_directory"
cmd="mkdir -p $remote_directory && rm $remtoe_directory/deps -rf"
echo "ssh rcmu@c200.taomu.cool $cmd"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c200.taomu.cool $cmd
echo "ssh rcmu@c221.taomu.cool $cmd"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c221.taomu.cool $cmd
echo "ssh rcmu@c202.taomu.cool $cmd"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c202.taomu.cool $cmd
echo "ssh rcmu@c201.taomu.cool $cmd"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c201.taomu.cool $cmd
echo "ssh rcmu@c223.taomu.cool $cmd"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c223.taomu.cool $cmd

echo "scp taomu.tar.gz rcmu@c200.taomu.cool:$remote_directory"
sshpass -p $pass scp -o StrictHostKeyChecking=no taomu.tar.gz rcmu@c200.taomu.cool:$remote_directory
echo "scp taomu.tar.gz rcmu@c221.taomu.cool:$remote_directory"
sshpass -p $pass scp -o StrictHostKeyChecking=no taomu.tar.gz rcmu@c221.taomu.cool:$remote_directory
echo "scp taomu.tar.gz rcmu@c201.taomu.cool:$remote_directory"	
sshpass -p $pass scp -o StrictHostKeyChecking=no taomu.tar.gz rcmu@c201.taomu.cool:$remote_directory
echo "scp taomu.tar.gz rcmu@c202.taomu.cool:$remote_directory"	
sshpass -p $pass scp -o StrictHostKeyChecking=no taomu.tar.gz rcmu@c202.taomu.cool:$remote_directory
echo "scp taomu.tar.gz rcmu@c223.taomu.cool:$remote_directory"
sshpass -p $pass scp -o StrictHostKeyChecking=no taomu.tar.gz rcmu@c223.taomu.cool:$remote_directory

cmd2="cd $remote_directory && tar xvf taomu.tar.gz && ls -l" 
echo "ssh rcmu@c200.taomu.cool $cmd2"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c200.taomu.cool $cmd2
echo "ssh rcmu@c221.taomu.cool $cmd2"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c221.taomu.cool $cmd2
echo "ssh rcmu@c202.taomu.cool $cmd2"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c202.taomu.cool $cmd2
echo "ssh rcmu@c201.taomu.cool $cmd2"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c201.taomu.cool $cmd2
echo "ssh rcmu@c223.taomu.cool $cmd2"
sshpass -p $pass ssh -o StrictHostKeyChecking=no rcmu@c223.taomu.cool $cmd2

}


case "$1" in
    deploy)
        deploy
        ;;
    config)
        config
        ;;
    publish)
        publish
        ;;
    *)
        echo $"Usage: $0 {deploy|config|publish}"
        exit 1
esac
